package cn.wolfcode.web.controller;

import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;


  /*  select  b.name groupType, count(c.id) count, sum(c.total_amount) totalAmount, sum(c.pay_amount) payAmount,  sum(c.discount_amount) discountAmount
    from consumption c LEFT JOIN business b on c.business_id = b.id where c.status = 2
    group by  b.name;*/

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")ConsumptionReportQueryObject qo) {

        model.addAttribute("businesses", businessService.listAll());
        PageInfo pageInfo = consumptionReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumptionReport/list";
    }


/*
      x轴--->groupType   订单数  总消费金额    总优惠金额    总实收金额
    狼途汽车服务-广州天河店	    1	1500.00	    1500.00	    444.00
    狼途汽车服务-广州黄埔店	    1	0.00	    0.00	    0.00
    狼途汽车服务-江门新会店	    1	18260.00	18260.00	1057.81
    狼途汽车服务-深圳福田店	    2	7893.00	    7893.00	    290.32

    */
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo")ConsumptionReportQueryObject qo) {

        //List<Map<String, Object>> mapList = consumptionReportService.queryForChart(qo.getGroupType());
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo<Map<String, Object>> pageInfo = consumptionReportService.query(qo);
        //x轴--->groupType
        //['狼途汽车服务-广州天河店', '狼途汽车服务-广州黄埔店', '狼途汽车服务-江门新会店', '狼途汽车服务-深圳福田店'],
        List<Object> groupTypeList = new ArrayList<>();
        //总消费金额--分组维度为 门店 分组查询出来所有总消费金额 列
        //[2.0, 4.9, 7.0, 23.2]
        List<Object> totalAmountList = new ArrayList<>();
        //总优惠金额--分组维度为 门店 分组查询出来所有总优惠金额 列
        //[2.0, 4.9, 7.0, 23.2]
        List<Object> discountList = new ArrayList<>();
        //总实收金额-分组维度为 门店 分组查询出来所有总实收金额 列
        //[2.0, 4.9, 7.0, 23.2]
        List<Object> payAmountList = new ArrayList<>();
        //订单数-分组维度为 门店 分组查询出来所有订单数 列
        //[2.0, 4.9, 7.0, 23.2]
        List<Object> countList = new ArrayList<>();
        for (Map<String, Object> map : pageInfo.getList()) {
            groupTypeList.add(map.get("groupType"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountList.add(map.get("discountAmount"));
            countList.add(map.get("count"));
        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmountList",JSON.toJSONString( payAmountList));
        model.addAttribute("discountList",JSON.toJSONString( discountList));
        model.addAttribute("countList", JSON.toJSONString(countList));
        return "consumptionReport/listBar";
    }

}
